package com.clkj.cldisabledmanclient.mvp.homerecovertrain;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.widget.TextView;

import com.clkj.cldisabledmanclient.R;
import com.clkj.cldisabledmanclient.adapter.PhotoAdapter;
import com.clkj.cldisabledmanclient.app.CustomApplication;
import com.clkj.cldisabledmanclient.base.BaseActivity;
import com.clkj.cldisabledmanclient.bean.HomeTrainDetail;
import com.clkj.cldisabledmanclient.constant.ShareKey;
import com.clkj.cldisabledmanclient.util.CommonUtil;
import com.clkj.cldisabledmanclient.util.ToastUtil;
import com.clkj.cldisabledmanclient.util.scheduler.SchedulerProvider;

import java.util.ArrayList;
import java.util.List;

public class HomeTrainDetailActivity extends BaseActivity implements HomeRecoverTrainDetailContract.View {

    private TextView tvTrainTitle;
    private TextView tvStartDate;
    private RecyclerView rvPhotos;
    private TextView tvEndDate;
    private RecyclerView rvPhotos2;
    private TextView tvLongTime;

    private String trainDate;
    private String idCard;
    private HomeRecoverTrainDetailContract.Presenter mPresenter;

    private HomeTrainDetail homeTrainDetail;
    private ProgressDialog progressDialog;
    private PhotoAdapter photoAdapter;
    private PhotoAdapter photoAdapter2;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_home_train_detail);
        initTitle(null, null, "居家康复记录详情", true, null);
        initView();
        initData();
        assignView();

        getHomeRecoverTrainDetailDetail();
    }

    @Override
    public void initData() {

        idCard = (String) CommonUtil.getShareValue(ShareKey.USER_IDCARD, "");
        trainDate = getIntent().getStringExtra("trainDate");
        mPresenter = new HomeRecoverTrainDetailPresenter(this, SchedulerProvider.getInstance(), CustomApplication.getInstance().getHttpService());
    }

    @Override
    public void assignView() {

        GridLayoutManager llManager = new GridLayoutManager(this, 3);
        GridLayoutManager llManager2 = new GridLayoutManager(this, 3);
        llManager.setOrientation(LinearLayoutManager.VERTICAL);
        llManager2.setOrientation(LinearLayoutManager.VERTICAL);
        //解决scrollview和recycleview嵌套滑动冲突
        llManager.setSmoothScrollbarEnabled(true);
        llManager.setAutoMeasureEnabled(true);
        llManager2.setSmoothScrollbarEnabled(true);
        llManager2.setAutoMeasureEnabled(true);

        rvPhotos.setLayoutManager(llManager);
        rvPhotos.setNestedScrollingEnabled(false);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rvPhotos.setHasFixedSize(true);

        rvPhotos2.setLayoutManager(llManager2);
        rvPhotos2.setNestedScrollingEnabled(false);
        //如果可以确定每个item的高度是固定的，设置这个选项可以提高性能
        rvPhotos2.setHasFixedSize(true);
    }

    @Override
    public void initView() {

        tvTrainTitle = findViewById(R.id.tv_traintitle);
        tvStartDate = (TextView) findViewById(R.id.tv_startDate);
        rvPhotos = (RecyclerView) findViewById(R.id.rv_photos);
        tvEndDate = (TextView) findViewById(R.id.tv_endDate);
        rvPhotos2 = (RecyclerView) findViewById(R.id.rv_photos2);
        tvLongTime = findViewById(R.id.tv_longtime);
    }

    @Override
    public void getHomeRecoverTrainDetailDetail() {

        mPresenter.getHomeRecoverTrainDetail(idCard, trainDate);
    }

    @Override
    public void getHomeRecoverTrainDetailSuccess(HomeTrainDetail homeTrainDetail) {
        this.homeTrainDetail = homeTrainDetail;

        tvTrainTitle.setText(homeTrainDetail.getDate() + "日康复训练");
        tvStartDate.setText(homeTrainDetail.getStartTime());
        List<String> startFiles = new ArrayList<>();
        startFiles = homeTrainDetail.getStartFilePath();
        photoAdapter = new PhotoAdapter(startFiles, this);
        rvPhotos.setAdapter(photoAdapter);

        if (TextUtils.isEmpty(homeTrainDetail.getEndTime())) {
            tvEndDate.setText("训练未结束");
        } else {
            tvEndDate.setText(homeTrainDetail.getEndTime());
        }
        List<String> endFiles = new ArrayList<>();
        endFiles = homeTrainDetail.getEndFilePath();
        photoAdapter2 = new PhotoAdapter(endFiles, this);
        rvPhotos2.setAdapter(photoAdapter2);

        if (TextUtils.isEmpty(homeTrainDetail.getLongTime())) {
            tvLongTime.setText("训练未结束");
        } else {
            tvLongTime.setText(homeTrainDetail.getLongTime());
        }
    }

    @Override
    public void getHomeRecoverTrainDetailError(String tip) {

        ToastUtil.showShort(this, tip);
    }

    @Override
    public void setPresenter(HomeRecoverTrainDetailContract.Presenter presenter) {

        this.mPresenter = presenter;
    }

    @Override
    public void showLoading() {
        progressDialog = new ProgressDialog(this);
        progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        progressDialog.setIndeterminate(false);
        progressDialog.setCancelable(true);
        //设置点击进度对话框外的区域对话框不消失
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.setMessage("正在加载...");
        progressDialog.show();
    }

    @Override
    public void dismissLoading() {
        if (progressDialog != null) {
            progressDialog.dismiss();
            progressDialog = null;
        }
    }
}
